﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class dealersManage : System.Web.UI.Page
    {
        //BLL.Role rolebll = new BLL.Role();
        BLL.DealersManage dealersManagebll = new BLL.DealersManage();
        BLL.DealersGrade dealersGradebll = new BLL.DealersGrade();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((dealersManagebll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "confirm")
                {

                    Model.DealersManage ne = dealersManagebll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.userstate = "已审核";
                    ne.audituser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                    ne.audittime = DateTime.Now; ;
                    row = dealersManagebll.Update(ne) ? 2 : 0;
                    Response.Write(row);
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "selectgrade")
                {

                    //Model.DealersManage ne = dealersManagebll.GetModel(Convert.ToInt32(Request["grade"]));
                    //ne.userstate = "已审核";
                    //ne.audituser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                    //ne.audittime = DateTime.Now; ;
                    //row = dealersManagebll.Update(ne) ? 2 : 0;
                    Response.Write(dealersGradebll.GetModelList("grade='" + Request["grade"] + "'")[0].discountrate);
                }
                else
                {
                    Model.DealersManage ne = new Model.DealersManage();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        ne = dealersManagebll.GetModel(Convert.ToInt32(Request["id"]));
                        ne.username = Request["username"];
                        ne.mobile = Request["mobile"];
                        ne.s_province = Request["s_province"];
                        ne.s_city = Request["s_city"];
                        ne.s_county = Request["s_county"];
                        ne.description = Request["description"];
                        ne.dealersgrade = Convert.ToInt32(Request["dealersgrade"]);
                        ne.discountrate = Request["discountrate"];
                        row = dealersManagebll.Update(ne) ? 2 : 0;
                    }
                    else
                    {
                        ne.s_province = Request["s_province"];
                        ne.s_city = Request["s_city"];
                        ne.s_county = Request["s_county"];
                        ne.dealersid = "0";//0代表没有上级经销商 因为这里是总部新建的经销商 没有指定谁是它上级
                        ne.username = Request["username"];
                        ne.mobile = Request["mobile"];
                       
                        ne.dealersgrade = Convert.ToInt32(Request["dealersgrade"]);

                        ne.userstate = "待审核";
                        ne.description = Request["description"];
                        ne.createtime = DateTime.Now;
                        ne.moneybalance = 0;
                        ne.discountrate = Request["discountrate"];
                        row = dealersManagebll.Add(ne) > 0 ? 1 : 0;
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}